import os
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from Ui_Auto_Unittest.common.logger import FrameLog
from Ui_Auto_Unittest.common import dir_config

class BasePage:
    """
    BasePage:定义每个页面的相同属性及方法
    相同属性？获取浏览器驱动对象（数据）
    相同方法？元素定位、点击、输入...等等操作
    日志
    错误截图
    测试报告
    """
    def __init__(self,driver1):
        # 定义浏览器的属性
        self.driver=driver1

    def locator(self,page_name,loc):
        '''
        元素定位
        :param page_name:
        :param loc:
        :return:
        '''
        # WebElement对象
        try:
            el=self.driver.find_element(*loc)
            FrameLog().getLogger().info(f"在[{page_name}]，定位到元素：{loc}")

        except:
            FrameLog().getLogger().error(f"在[{page_name}]，未定位到元素：{loc}！！！")
            # 失败截图
            self.sava_page_shot(page_name)
            raise
        return el

    def input(self,page_name,loc,value):
        '''
        输入
        :param page_name:
        :param loc:
        :param value:
        :return:
        '''
        # 等待元素可见
        try:
            self.wait_ele_visibility(page_name,loc)
            self.locator(page_name,loc).send_keys(value)
            FrameLog().getLogger().info(f"在{page_name}，元素{loc}输入:{value}！")

        except:
            FrameLog().getLogger().error(f"在{page_name}，元素{loc}输入失败！")
            # 失败截图
            self.sava_page_shot(page_name)

    def click(self,page_name,loc):
        '''
        点击操作
        :param page_name:
        :param loc:
        :return:
        '''
        try:
            # 等待元素可见
            self.wait_ele_visibility(page_name,loc)
            self.locator(page_name,loc).click()
            FrameLog().getLogger().info(f"在[{page_name}]，点击元素{loc}成功！")
        except:
            FrameLog().getLogger().error(f"在[{page_name}]，元素{loc}点击失败！")
            # 失败截图
            self.sava_page_shot(page_name)

    def sleep(self,s):
        '''
        睡眠 /s
        :param s:
        :return:
        '''
        time.sleep(s)

    def move_element(self,page_name,loc):
        '''
        鼠标键盘操作
        :param page_name:
        :param loc:
        :return:
        '''
        try:
            # 等待元素可见
            self.wait_ele_visibility(page_name,loc)
            ActionChains(self.driver).move_to_element(self.locator(page_name,loc)).perform()
            FrameLog().getLogger().info(f"在[{page_name}]，鼠标移动到元素:{loc}")
        except:
            FrameLog().getLogger().error(f"在[{page_name}]，鼠标移动到元素{loc}失败！")
            # 失败截图
            self.sava_page_shot(page_name)

    # 等待元素存在
    def wait_ele_visibility(self,page_name,loc,timeout=15,poll_fre=0.5):
        try:
            WebDriverWait(self.driver,timeout,poll_fre).until(expected_conditions.visibility_of_element_located(loc))
            # WebElement对象
            FrameLog().getLogger().info(f"在[{page_name}]，找到元素：{loc}可见")
        except:
            FrameLog().getLogger().error(f"在[{page_name}]，未找到元素：{loc}可见！！！")

    # 等待元素可见
    def wait_ele_presence(self,page_name,loc,timeout=15,poll_fre=0.5):
        try:
            WebDriverWait(self.driver,timeout,poll_fre).until(expected_conditions.presence_of_element_located(loc))
            FrameLog().getLogger().info(f"在[{page_name}]，找到元素：{loc}存在")
        except:
            FrameLog().getLogger().error(f"在[{page_name}]，未找到元素：{loc}存在！！！")


    def sava_page_shot(self,img_name):
        '''
        截图操作
        :param img_name:
        :return:
        '''
        filename=os.path.join(dir_config.screen_dir,img_name+'.png')
        self.driver.save_screenshot(filename)
        FrameLog().getLogger().info(f"失败截图，截取当前网页，存储的路径:{filename}")

# 日志+错误截图+测试报告输出
